/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.BusinessConfig;
import com.boxin.ims.modules.wechat.entity.WechatBusiness;
import com.boxin.ims.modules.wechat.service.BusinessConfigService;
import com.boxin.ims.modules.wechat.service.WechatBusinessService;

/**
 * 微信业务Controller
 * @author Jakemanse
 * @version 2013-10-23
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/wechatBusiness")
public class WechatBusinessController extends BaseController {

	@Autowired
	private WechatBusinessService wechatBusinessService;
	
	@Autowired
	private BusinessConfigService businessConfigService;
	
	@ModelAttribute
	public WechatBusiness get(@RequestParam(required=false) Long id) {
		if (id != null){
			return wechatBusinessService.get(id);
		}else{
			return new WechatBusiness();
		}
	}
	
	@RequiresPermissions("wechat:wechatBusiness:view")
	@RequestMapping(value = {"list", ""})
	public String list(WechatBusiness wechatBusiness, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			wechatBusiness.setUser(user);
		}
        Page<WechatBusiness> page = wechatBusinessService.find(new Page<WechatBusiness>(request, response), wechatBusiness); 
        model.addAttribute("page", page);
		return "modules/wechat/wechatBusinessList";
	}

	@RequiresPermissions("wechat:wechatBusiness:view")
	@RequestMapping(value = "form")
	public String form(WechatBusiness wechatBusiness, Model model) {
		
		
		WechatBusiness temp = wechatBusinessService.getByUserId(wechatBusiness.getUser().getId());
		if(temp != null){
			wechatBusiness = temp;
		}else{
			
			if(wechatBusiness.getUser()==null){
				wechatBusiness.setUser(UserUtils.getUser());
			}
		}
		
		List<BusinessConfig> configList = 	businessConfigService.findAll();	//所有服务配置
		model.addAttribute("configList", configList);
		model.addAttribute("wechatBusiness", wechatBusiness);
		
		
		return "modules/wechat/wechatBusinessForm";
	}

	@RequiresPermissions("wechat:wechatBusiness:edit")
	@RequestMapping(value = "save")
	public String save(WechatBusiness wechatBusiness, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, wechatBusiness)){
			return form(wechatBusiness, model);
		}
		wechatBusinessService.save(wechatBusiness);
		addMessage(redirectAttributes, "保存微信业务成功");
//		return "redirect:"+Global.ADMIN_PATH+"/wechat/wechatBusiness/?repage";
		return form(wechatBusiness, model);
	}
	
	@RequiresPermissions("wechat:wechatBusiness:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		wechatBusinessService.delete(id);
		addMessage(redirectAttributes, "删除微信业务成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/wechatBusiness/?repage";
		
	}

}
